home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / appl / qwriter / nfs.c < prev    next >
C/C++ Source or Header  |  1993-12-21  |  5KB  |  191 lines

  1. /*
  2.  * NFS-test routines
  3.  * $Header: /u/opi/86/jraja/ohtatcp/amitcp/src/appl/qwriter/RCS/nfs.c,v 2.2 1993/12/21 21:34:02 jraja Exp $
  4.  * $Log: nfs.c,v $
  5.  * Revision 2.2  1993/12/21  21:34:02  jraja
  6.  * Fixed checksum data length + various other small fixes.
  7.  *
  8.  * Revision 2.1  93/04/13  21:46:10  21:46:10  jraja (Jarno Tapio Rajahalme)
  9.  * Made this compile with the newest API.
  10.  * 
  11.  * Revision 2.0  93/03/20  17:31:45  17:31:45  ppessi (Pekka Pessi)
  12.  * initial netlib version..
  13.  * 
  14.  * Revision 1.10  93/03/19  14:45:10  14:45:10  puhuri (Markus Peuhkuri)
  15.  * Add modifications made by too and ppessi.
  16.  * 
  17.  * Revision 1.9  93/03/17  21:24:02  21:24:02  puhuri (Markus Peuhkuri)
  18.  * Add checksum test
  19.  * 
  20.  * Revision 1.8  93/03/16  19:14:01  19:14:01  too (Tomi Ollila)
  21.  * code fixes
  22.  * 
  23.  * Revision 1.7  93/03/16  10:42:33  10:42:33  puhuri (Markus Peuhkuri)
  24.  * Added AmiTCP stuff.
  25.  * 
  26.  * Revision 1.6  93/03/15  18:59:37  18:59:37  puhuri (Markus Peuhkuri)
  27.  * Comment fix
  28.  * 
  29.  */
  30.  
  31. /*
  32.  * A testing software for UDP-ping-pong
  33.  * Client sends a UDP-datagram to server, which responds with
  34.  * same datagram. At server end there is a time control: 
  35.  * if server does not respond in NFSTIMEOUT (30) seconds
  36.  * one of packets is asumed to disapperad and another packet is sent.
  37.  * After
  38.  */
  39.  
  40. #ifdef AMIGA
  41. #if __SASC
  42. #include <proto/socket.h>
  43. #elif __GNUC__
  44. #include <inline/socket.h>
  45. #else
  46. #include <clib/socket_protos.h>
  47. #endif
  48. #endif /* AMIGA */
  49.  
  50. #ifdef __STDC__
  51. #include <stdlib.h>
  52. #endif
  53.  
  54. #include <stdio.h>
  55.  
  56. #include <sys/types.h>
  57. #include <sys/socket.h>
  58. #include <sys/ioctl.h>
  59.  
  60. #include <sys/time.h>
  61.  
  62. #include <netinet/in.h>
  63.  
  64. #include "qwriter.h"
  65. #include <string.h>        /* after qwriter.h */
  66.  
  67. int nfs_server(char *host, UWORD port1, int bufsiz, int length, int chk)
  68. {
  69.   int sid_s;
  70.   struct sockaddr who;
  71.   long adrlen = sizeof(who);
  72.   int rounds=0;
  73.   u16 *buf, sum;
  74.  
  75.  
  76.   int r,s;
  77.   if((sid_s=open_server(NULL, port1, SOCK_DGRAM))<0) /* We listen this */
  78.     return(FAIL);
  79.   
  80.   if(buf=(u16 *)malloc(max(bufsiz, length))){    /* Allocate buffer */
  81.     *buf=length;
  82.     for(;;){
  83.       if((r=recvfrom(sid_s, (char *)buf, bufsiz, 0, &who, &adrlen)) < 0)
  84.     preturn("nfs-server: recvfrom()");
  85.       if(*buf == 0)
  86.     break;
  87.       if (r != ntohs(*buf)) {
  88.     fprintf(stderr, "Message size mismatch (was: %ld, should be: %ld)\n",
  89.         r, ntohs(*buf));
  90.     goto barf;
  91.       }
  92.       if(chk)
  93.     if(ntohs(*(buf+1)) != 
  94.        (sum = pppfcs(PPPINITFCS, (unsigned char *)(buf+2), ntohs(*buf)-4)))
  95.       fprintf(stderr,"Checksum error in nfs-server: "
  96.           "should be %ld, is %ld\n"
  97.           "Error in received packet %ld\n",ntohs(*(buf+1)),
  98.           sum, rounds+1);
  99.       *buf=htons(length);
  100.       if(chk)
  101.     *(buf+1) = htons(pppfcs(PPPINITFCS, (unsigned char *)(buf+2), length-4));
  102.       if((s=sendto(sid_s, (char *)buf, length, 0, &who, adrlen)) < 0)
  103.     preturn("nfs-server: write");
  104.       if (s != length) {
  105.     fprintf(stderr, "Message size mismatch (sendto())(was: %ld, should be: %ld)\n",
  106.         s, length);
  107.     break;
  108.       }
  109.       DP(("r=%ld s=%ld len=%ld buf=%ld\n",r,s,length, bufsiz));
  110.       rounds++;
  111.     }
  112. barf:
  113.     CloseSocket(sid_s);
  114.     printf("Sent packet %ld times\n",rounds);
  115.     free(buf);
  116.   }
  117.   return(OK);
  118. }
  119.  
  120. int nfs_client(char *host, UWORD port1, int rounds, int length, 
  121.            int bufsiz, double tval, int chk)
  122. {
  123.   int sid_c, i;
  124.   u16 *buf, sum;
  125.   struct timeval tv, tv1, tv2;
  126.   double timediff;
  127.   int fail = 0;
  128.   fd_set reades;
  129.  
  130.   int r,s;
  131.  
  132.   if((sid_c = open_client(host, port1, SOCK_DGRAM)) < 0) /* We send there */
  133.     return(FAIL);
  134.  
  135.   if(buf=(u16 *)malloc(max(bufsiz, length))){    /* Allocate junk-buffer */
  136.     tv.tv_sec=(int)tval;tv.tv_usec=(int)((tval-tv.tv_sec)*1E6);
  137.     gettimeofday(&tv1,NULL);
  138.     for(i=0;i<rounds ;i++){
  139.       do {
  140.     *buf=htons(length);
  141.     if(chk)
  142.       *(buf+1) = 
  143.         htons(pppfcs(PPPINITFCS, (unsigned char *)(buf+2), length-4));
  144.     if((s = send(sid_c, (char *)buf, length, 0))<0)
  145.       preturn("nfs-client: send");
  146.     if (s != length) {
  147.       fprintf(stderr, "Message size mismatch (send())(was: %ld, should be: %ld)\n",
  148.           s, length);
  149.       break;
  150.     }
  151.     FD_ZERO(&reades);
  152.     FD_SET(sid_c, &reades);
  153.       } while (select(sid_c + 1, &reades, NULL, NULL, &tv) == 0 && ++fail);
  154.       if((r = recv(sid_c, (char *)buf, bufsiz, 0)) < 0)
  155.     preturn("nfs-client: recv");
  156.       if (r != ntohs(*buf)) {
  157.     fprintf(stderr, "Message size mismatch (was: %ld, should be: %ld)\n",
  158.         r, ntohs(*buf));
  159.     break;
  160.       }
  161.       if(chk)
  162.     if(ntohs(*(buf+1)) != 
  163.        (sum = pppfcs(PPPINITFCS, (unsigned char *)(buf+2), ntohs(*buf)-4)))
  164.       fprintf(stderr,"Checksum error in nfs-client: "
  165.           "should be %ld, is %ld\n"
  166.           "Error in received packet %ld\n",ntohs(*(buf+1)),
  167.           sum, i+1);
  168.       DP(("s=%ld r=%ld i=%ld fail=%ld\n",s,r,i,fail));
  169.     }
  170.     *buf=0;
  171.     if((s = send(sid_c, (char *)buf, 2, 0)) < 0)
  172.       preturn("nfs-client: write");
  173.     
  174.     gettimeofday(&tv2,NULL);
  175.     free(buf);
  176.   }
  177.   CloseSocket(sid_c);
  178.  
  179.   timediff = (double)(tv2.tv_sec-tv1.tv_sec)+
  180.     ((double)(tv2.tv_usec-tv1.tv_usec))/1E6;
  181.   printf("Send %ld (%ld retransmissions), got %ld packets \n",i+fail,fail,i);
  182.   printf("Total time used: %8.4lf seconds (%lg bytes/s)\n",
  183.      timediff,(i * (length + r)) / timediff);
  184.   printf("Real time used: %8.4lf seconds (%lg bytes/s)\n",
  185.      timediff - (tval * fail),
  186.      (i * (length + r)) / (timediff - (tval * fail)));
  187.   printf("Round-trip time: %8.4lf ms (total), %8.4lf ms (real)\n",
  188.      1000 * timediff / i, 1000 * (timediff - tval * fail) / i);
  189.   return(OK);
  190. }
  191.